﻿define([
    'Xjs/core/util',
    'Xjs/ui/panel/Panel',
    'Xjs/ui/grid/PropertyRow',
],
function (util, Panel) {
    'use strict';

    var type = 'propertyGroup',
    PropertyGroup = Xjs.getType(type);

    if (PropertyGroup) { // It has been already loaded
        return PropertyGroup;
    }

    PropertyGroup = util.define({
        base: Panel,
        construct: function (conf, parent) {

            var me = this,
                prop,
                row;

            PropertyGroup.base.constructor.call(me, conf, parent); // Call the base constructor

            if (conf.label) {
                me.createHeader(conf.label);
                me.header.replaceClass('w-header', 'w-subHeader');
            }

            if (conf.fields) {

                if (!me.body) {
                    me.body = Xjs.create('control', {}, me);
                }

                conf.fields.forEach(function (propConf) {

                    for (var k in propConf) {
                        prop = propConf[k];
                        prop.name = k; // Set the name of the property
                        row = Xjs.create('propertyRow', prop, me);
                        me.body.addChild(row);
                    }
                });
            }
        },

        type: type,

        border: false,

        addRow: function (row) {
        }
    });

    Xjs.registerType(PropertyGroup);

    return PropertyGroup;
});